package com.qk.controller;


import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Activity;
import com.qk.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping ("/activities")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @GetMapping
    public Result pageActivities(@RequestParam(required = false) String channel,
                                 @RequestParam(required = false) String type,
                                 @RequestParam(required = false) String status,
                                 @RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("条件分页查询活动：channel={}，type={},status={},page={},pageSize={}", channel, type, status, page, pageSize);
        PageResult<Activity> pageResult = activityService.pageActivities(channel, type, status, page, pageSize);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("删除活动：id={}", id);
        activityService.removeById(id);
        return Result.success();
    }

    @PostMapping
    public Result addActivity(@RequestBody Activity activity) {
        log.info("添加活动：{}", activity);
        activityService.saveActivity(activity);
        return Result.success();
    }

    @GetMapping ("/{id}")
    public Result getActivityById(@PathVariable Integer id) {
        log.info("查询活动：id={}", id);
        Activity activity = activityService.getActivityById(id);
        return Result.success(activity);
    }

    @PutMapping
    public Result updateActivity(@RequestBody Activity activity) {
        log.info("修改活动：{}", activity);
        activityService.updateActivity(activity);
        return Result.success();
    }

    @GetMapping("/type/{type}")
    public Result listActivitiesByType(@PathVariable String type) {
        log.info("查询活动：type={}", type);
        List<Activity> activities = activityService.listActivitiesByType(type);
        return Result.success(activities);
    }
}
